/**
 * DataGenerateCenter.java [V 1.0.0]
 * classes : com.xiaocq.wordsgod.db.DataGenerateCenter
 * xiaocq Create at 2012-7-4 下午8:57:26
 */
package com.xiaocq.wordsgod.db;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.TreeSet;

import android.content.Context;
import android.database.Cursor;

import com.xiaocq.wordsgod.model.Control;
import com.xiaocq.wordsgod.model.Vocabulary;

/**
 * com.xiaocq.wordsgod.db.DataGenerateCenter
 * @author Mystory
 * Create at 2012-7-4 下午8:57:26
 */
public class DataGenerateCenter {
	public static final int MAX_WORD_COUNT = 3632;
	private Control mControl;
	private Context mContext;
	
	private WordSetUtil wordSetUtil;
	private Cursor cursor;
	
	private boolean isNewTest;
	
	private Random random;
	
	public DataGenerateCenter(Control control, Context context){
		this.mContext = context;
		this.mControl = control;
		
		this.wordSetUtil = new WordSetUtil(mContext);
		wordSetUtil.open();
		
		random = new Random();
		
		isNewTest = mControl.isNewTest();
		if(isNewTest){
			int[] wordScore = getRandomWordList(mControl.getTestId(), mControl.getTotalNum());
			mControl.setWordScore(wordScore);
		}
	}
	
	private int[] getRandomWordList(int testId, int totalNum){
		TreeSet<Integer> hashSet = new TreeSet<Integer>();
		
		int[] wordScore = new int[totalNum];
		int finished = 0;
		int range;
		int position;
		
		Cursor idSet;
		
		idSet = wordSetUtil.getWordIdList(testId);
		range = idSet.getCount();
		
		while(finished < totalNum){
			position = random.nextInt(range);
			if(hashSet.contains(position) == false){
				hashSet.add(position);
				
				idSet.moveToPosition(position);
				
				wordScore[finished++] = idSet.getInt(0);
			}
		}
		
		idSet.close();
		
		return wordScore;
	}
	
	/**
	 * 生成对应的测试单词列表
	 * @return
	 */
	public List<Vocabulary> wrapWordList(){
		List<Vocabulary> wordList = null;
		
		boolean testType = mControl.getTestType();
		int[] wordScore = mControl.getWordScore();
		int testedNum = mControl.getTestedNum();
		int restTestNum = mControl.getRestTestNum();
		int position = 0;
		String english;
		String[] chinese;
		int id;
		
		createCursor();
		
		wordList = new ArrayList<Vocabulary>(restTestNum);
		
		while(position < restTestNum){
			cursor.moveToPosition(wordScore[position + testedNum] - 1);
			id = cursor.getInt(0);
			english = cursor.getString(1);
			if(testType == Control.CHOICE){
				chinese = wrapChinese(id);
			}else{
				chinese = cursor.getString(2).split("\\|");
			}
			Vocabulary vocabulary = new Vocabulary(english, chinese, id);
			wordList.add(vocabulary);
		}
		close();
		return wordList;
	}
	
	public Vocabulary wrapVocabulary(int position, boolean testType){
		cursor.moveToPosition(position - 1);
		int id = cursor.getInt(0);
		String english = cursor.getString(1);
		String[] chinese;
		
		String tmpString;
		
		if(testType == Control.CHOICE){
			chinese = wrapChinese(id);
		}else{
			tmpString = cursor.getString(2);
			chinese = new String[]{tmpString.replaceAll("\\|", "\n")};
		}
		Vocabulary vocabulary = new Vocabulary(english, chinese, id);
		return vocabulary;
	}
	
	public void close(){
		cursor.close();
		wordSetUtil.close();
	}
	
	public void createCursor(){
		if(cursor == null){
			cursor = wordSetUtil.getAllWord();
		}
	}
	
	private String[] wrapChinese(int id){
		String[] chinese = new String[4];
		
		for(int i=0; i<4; i++){
			chinese[i] = getChinese(i, id);
		}
		
		return chinese;
	}
	
	private String getChinese(int index, int id){
		int position;
		String[] cArray;
		if(index == (id % 4)){
			position = id - 1;
		}else{
			do{
				position = random.nextInt(MAX_WORD_COUNT);
			}while(position == (id - 1));
		}
		cursor.moveToPosition(position);
		cArray = cursor.getString(2).split("\\|");
		return cArray[random.nextInt(cArray.length)];
	}
	
	public void updateWordState(int[] wordScore){
		wordSetUtil.updateToDatabase(wordScore);
	}
}
